/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.arg.deploy.webapp.common;

//import com.arg.dbd.app.common.DAOFactory;
//import com.arg.dbd.app.dao.UserDAO;
//import com.arg.dbd.common.KeyConverter;
//import com.arg.dbd.model.sec.User;
import java.math.BigDecimal;
import java.util.Date;
//import nl.captcha.Captcha;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Checkbox;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Datebox;
import org.zkoss.zul.Decimalbox;
import org.zkoss.zul.Doublebox;
import org.zkoss.zul.Intbox;
import org.zkoss.zul.Label;
import org.zkoss.zul.Longbox;
import org.zkoss.zul.Radio;
import org.zkoss.zul.Radiogroup;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Timebox;

/**
 *
 * @author kasidit
 */
public class ValidateUtil {

    private static Log LOG = LogFactory.getLog(ValidateUtil.class);
    public static final String INVALID = "outline: 1px solid red; background: #FFF;";
    public static final String VALID = null;

    public static Boolean validRowOther(Checkbox chk, Textbox txtbox) {
        if (chk.isChecked()) {
            if (txtbox.getText().equals("")) {
                txtbox.setStyle(ValidateUtil.INVALID);
                return Boolean.FALSE;
            }
        }
        return Boolean.TRUE;
    }

    public static Validation requireField(Component field1) {
        return requireField(field1, null);
    }

    public static Validation requireField(Component field1, String msg) {
        Validation result = new Validation(Validation.VALIDATION_ID_REQUIRE);
        if (field1 instanceof Combobox) {
            Combobox field = (Combobox) field1;
            if (msg != null) {
                return requireField(field, msg, null);
            } else {
                return requireField(field);
            }
        } else if (field1 instanceof Textbox) {
            Textbox field = (Textbox) field1;
            if (msg != null) {
                return requireField(field, msg);
            } else {
                return requireField(field);
            }
        } else if (field1 instanceof Longbox) {
            Longbox field = (Longbox) field1;
            if (msg != null) {
                return requireField(field, msg);
            } else {
                return requireField(field);
            }
        } else if (field1 instanceof Doublebox) {
            Doublebox field = (Doublebox) field1;
            if (msg != null) {
                return requireField(field, msg);
            } else {
                return requireField(field);
            }
        } else if (field1 instanceof Decimalbox) {
            Decimalbox field = (Decimalbox) field1;
            if (msg != null) {
                return requireField(field, msg);
            } else {
                return requireField(field);
            }
        } else if (field1 instanceof Intbox) {
            Intbox field = (Intbox) field1;
            if (msg != null) {
                return requireField(field, msg);
            } else {
                return requireField(field);
            }
        } else if (field1 instanceof Datebox) {
            Datebox field = (Datebox) field1;
            if (msg != null) {
                return requireField(field, msg);
            } else {
                return requireField(field);
            }
        } else if (field1 instanceof Radiogroup) {
            Radiogroup field = (Radiogroup) field1;
            if (msg != null) {
                return requireField(field, msg);
            } else {
                return requireField(field);
            }
        } else if (field1 instanceof Label) {
            Label field = (Label) field1;
            if (msg != null) {
                return requireField(field, msg);
            } else {
                return requireField(field);
            }
        } else if (field1 instanceof Checkbox) {
            Checkbox field = (Checkbox) field1;
            if (msg != null) {
                return requireField(field, msg);
            } else {
                return requireField(field);
            }
        }
        return result;
    }

    public static Validation requireField(Textbox field1) {
        Validation result = new Validation(Validation.VALIDATION_ID_REQUIRE);
        String message = null;
        Boolean value = true;
        String data = field1.getValue();
        if (data == null) {
            value = false;
        } else {
            if (StringUtils.isEmpty(data.trim())) {
                value = false;
            }
        }
        if (value) {
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.TRUE));
        } else {
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.FALSE));
            boolean addEventListener = field1.addEventListener(Events.ON_CHANGE, new ValidTextLongDoubleBoxEventListener(field1));
            //LOG.info(addEventListener);
            message = ResourceUtil.getLabel("validate.message.require");
        }
        result.setValue(value);
        result.setMessage(message);
        return result;
    }

    public static Validation requireFieldDecimalboxCustomMessage(Decimalbox field1, String propertiesValue) {
        Validation result = new Validation(Validation.VALIDATION_ID_REQUIRE);
        String message = null;
        Boolean value = true;
        BigDecimal data = field1.getValue();
        if (data == null) {
            value = false;
        } else {
            value = true;
        }
        if (value) {
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.TRUE));
        } else {
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.FALSE));
            boolean addEventListener = field1.addEventListener(Events.ON_CHANGE, new ValidDecimalboxEventListener(field1));
            message = ResourceUtil.getLabel(propertiesValue);
        }
        result.setValue(value);
        result.setMessage(message);
        return result;
    }

    public static Validation requireFieldTextboxCustomMessage(Textbox field1, String propertiesValue) {
        Validation result = new Validation(Validation.VALIDATION_ID_REQUIRE);
        String message = null;
        Boolean value = true;
        String data = field1.getValue();
        if (data == null) {
            value = false;
        } else {
            if (org.apache.commons.lang.StringUtils.isEmpty(data.trim())) {
                value = false;
            }
        }
        if (value) {
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.TRUE));
        } else {
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.FALSE));
            boolean addEventListener = field1.addEventListener(Events.ON_CHANGE, new ValidTextLongDoubleBoxEventListener(field1));
            //LOG.info(addEventListener);
            message = ResourceUtil.getLabel(propertiesValue);
        }
        result.setValue(value);
        result.setMessage(message);
        return result;
    }

    public static Validation requireFieldDateBoxCustomMessage(Datebox field1, String propertiesValue) {
        Validation result = new Validation(Validation.VALIDATION_ID_REQUIRE);
        String message = null;
        Boolean value = true;
        Date data = field1.getValue();
        if (data == null) {
            value = false;
        }
        if (value) {
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.TRUE));
        } else {
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.FALSE));
            field1.addEventListener(Events.ON_CHANGE, new ValidDateboxEventListener(field1));
            message = ResourceUtil.getLabel(propertiesValue);
        }
        result.setValue(value);
        result.setMessage(message);
        return result;
    }

    public static Validation requireFieldComboBoxCustomMessage(Combobox cmd, String propertiesValue) {
        Validation result = new Validation(Validation.VALIDATION_ID_REQUIRE);
        String message = null;
        Boolean value = true;
        int data = cmd.getSelectedIndex();
        if (data == 0 || data == -1) {
            if (data == -1) {
                value = false;
            } else {
                if (cmd.getSelectedItem() != null) {
                    if (cmd.getSelectedItem().getValue() == null
                            || "".equals(cmd.getSelectedItem().getValue())) {
                        value = false;
                    }
                }
            }
        }
        if (value) {
            cmd.setStyle(setStyleEdit(cmd.getStyle(), Boolean.TRUE));
        } else {
            cmd.setStyle(setStyleEdit(cmd.getStyle(), Boolean.FALSE));
            boolean addEventListener = cmd.addEventListener(Events.ON_CHANGE, new ValidComboboxEventListener(cmd));
            message = ResourceUtil.getLabel(propertiesValue);
        }
        result.setValue(value);
        result.setMessage(message);
        return result;
    }

    public static Validation requireField(String typePattern, Textbox textbox) {
        Validation result = new Validation(Validation.VALIDATION_ID_REQUIRE);
        String message = null;
        Boolean value = false;
        if (null != textbox.getValue()) {
            Pattern pattern = Pattern.compile(typePattern);
            Matcher matcher = pattern.matcher(textbox.getValue().trim());
            if (matcher.matches()) {
                value = true;
            } else {
                value = false;
            }
        } else {
            value = false;
        }
        if (value) {
            textbox.setStyle(setStyleEdit(textbox.getStyle(), Boolean.TRUE));
        } else {
            textbox.setStyle(setStyleEdit(textbox.getStyle(), Boolean.FALSE));
            boolean addEventListener = textbox.addEventListener(Events.ON_CHANGE, new ValidTextLongDoubleBoxEventListener(textbox));
            message = ResourceUtil.getLabel("validate.message.require");
        }
        result.setValue(value);
        result.setMessage(message);
        return result;
    }

    public static Validation requireField(String typePattern, Decimalbox decimalbox) {
        Validation result = new Validation(Validation.VALIDATION_ID_REQUIRE);
        String message = null;
        Boolean value = false;
        if (null != decimalbox.getValue()) {
            Pattern pattern = Pattern.compile(typePattern);
            Matcher matcher = pattern.matcher(decimalbox.getValue().toString());

            if (matcher.matches()) {
                value = true;
            } else {
                value = false;
            }
        } else {
            value = false;
        }
        if (value) {
            decimalbox.setStyle(VALID);
        } else {
            decimalbox.setStyle(INVALID);
            boolean addEventListener = decimalbox.addEventListener(Events.ON_CHANGE, new ValidTextLongDoubleBoxEventListener(decimalbox));
            message = ResourceUtil.getLabel("validate.message.require");
        }
        result.setValue(value);
        result.setMessage(message);
        return result;
    }

    public static Validation requireField(String typePattern, Intbox intbox) {
        Validation result = new Validation(Validation.VALIDATION_ID_REQUIRE);
        String message = null;
        Boolean value = false;
        if (null != intbox.getValue()) {
            Pattern pattern = Pattern.compile(typePattern);
            Matcher matcher = pattern.matcher(intbox.getValue().toString());
            if (matcher.matches()) {
                value = true;
            } else {
                value = false;
            }
        } else {
            value = false;
        }
        if (value) {
            intbox.setStyle(VALID);
        } else {
            intbox.setStyle(INVALID);
            boolean addEventListener = intbox.addEventListener(Events.ON_CHANGE, new ValidTextLongDoubleBoxEventListener(intbox));
            message = ResourceUtil.getLabel("validate.message.require");
        }
        result.setValue(value);
        result.setMessage(message);
        return result;
    }

    public static Validation requireField(String typePattern, Longbox longbox) {
        Validation result = new Validation(Validation.VALIDATION_ID_REQUIRE);
        String message = null;
        Boolean value = false;
        if (null != longbox.getValue()) {
            Pattern pattern = Pattern.compile(typePattern);
            Matcher matcher = pattern.matcher(longbox.getValue().toString());
            if (matcher.matches()) {
                value = true;
            } else {
                value = false;
            }
        } else {
            value = false;
        }
        if (value) {
            longbox.setStyle(VALID);
        } else {
            longbox.setStyle(INVALID);
            boolean addEventListener = longbox.addEventListener(Events.ON_CHANGE, new ValidTextLongDoubleBoxEventListener(longbox));
            message = ResourceUtil.getLabel("validate.message.require");
        }
        result.setValue(value);
        result.setMessage(message);
        return result;
    }

    public static Validation requireField(String typePattern, Datebox datebox, Textbox textbox) {
        Validation result = new Validation(Validation.VALIDATION_ID_REQUIRE);
        String message = null;
        Boolean value = false;
        if (null != textbox.getValue()) {
            Pattern pattern = Pattern.compile(typePattern);
            Matcher matcher = pattern.matcher(textbox.getValue().toString().trim());
            if (matcher.matches()) {
                value = true;
            } else {
                value = false;
            }
        } else {
            value = false;
        }
        if (value) {
            datebox.setStyle(VALID);
        } else {
            datebox.setStyle(INVALID);
            boolean addEventListener = datebox.addEventListener(Events.ON_CHANGE, new ValidTextLongDoubleBoxEventListener(datebox));
            message = ResourceUtil.getLabel("validate.message.require");
        }
        result.setValue(value);
        result.setMessage(message);
        return result;
    }

    public static Validation requireField(Longbox field1) {
        Validation result = new Validation(Validation.VALIDATION_ID_REQUIRE);
        String message = null;
        Boolean value = true;
        Long data = field1.getValue();
        if (data == null) {
            value = false;
        }

        if (value) {
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.TRUE));
        } else {
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.FALSE));
            boolean addEventListener = field1.addEventListener(Events.ON_CHANGE, new ValidTextLongDoubleBoxEventListener(field1));
            //LOG.info(addEventListener);
            message = ResourceUtil.getLabel("validate.message.require");
        }
        result.setValue(value);
        result.setMessage(message);
        return result;
    }

    public static Validation requireField(Doublebox field1) {
        Validation result = new Validation(Validation.VALIDATION_ID_REQUIRE);
        String message = null;
        Boolean value = true;
        Double data = field1.getValue();
        if (data == null) {
            value = false;
        }

        if (value) {
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.TRUE));
        } else {
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.FALSE));
            boolean addEventListener = field1.addEventListener(Events.ON_CHANGE, new ValidTextLongDoubleBoxEventListener(field1));
            //LOG.info(addEventListener);
            message = ResourceUtil.getLabel("validate.message.require");
        }
        result.setValue(value);
        result.setMessage(message);
        return result;
    }

    public static Validation requireField(Textbox field1, String msg) {
        Validation result = new Validation(Validation.VALIDATION_ID_REQUIRE);
        String message = null;
        Boolean value = true;
        String data = field1.getValue();
        if (data == null) {
            value = false;
        } else {
            if ("".equals(data.trim())) {
                value = false;
            }
        }
        if (value) {
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.TRUE));
        } else {
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.FALSE));
            boolean addEventListener = field1.addEventListener(Events.ON_CHANGE, new ValidTextLongDoubleBoxEventListener(field1));
            //LOG.info(addEventListener);
            message = msg;
        }
        LOG.debug("Massage : " + message);
        result.setValue(value);
        result.setMessage(message);
        return result;
    }

    public static Validation requireField(Decimalbox field1) {
        Validation result = new Validation(Validation.VALIDATION_ID_REQUIRE);
        String message = null;
        Boolean value = true;
        BigDecimal data = field1.getValue();
        if (data == null) {
            value = false;
        } else {
            value = true;
        }
        if (value) {
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.TRUE));
        } else {
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.FALSE));
            boolean addEventListener = field1.addEventListener(Events.ON_CHANGE, new ValidDecimalboxEventListener(field1));
            //LOG.info(addEventListener);
            message = ResourceUtil.getLabel("validate.message.require");
        }
        result.setValue(value);
        result.setMessage(message);
        return result;
    }

    public static Validation requireFieldSpecialCase(Decimalbox field1) {
        Validation result = new Validation(Validation.VALIDATION_ID_REQUIRE);
        String message = null;
        Boolean value = true;
        BigDecimal data = field1.getValue();
        if (data == null || data.compareTo(BigDecimal.ZERO) <= 0) {
            value = false;
        } else {
            value = true;
        }
        if (value) {
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.TRUE));
        } else {
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.FALSE));
            boolean addEventListener = field1.addEventListener(Events.ON_CHANGE, new ValidDecimalboxEventListener(field1));
            //LOG.info(addEventListener);
            message = ResourceUtil.getLabel("validate.message.require");
        }
        result.setValue(value);
        result.setMessage(message);
        return result;
    }

    public static Validation requireField(Longbox lngBox, String str) {
        Validation result = new Validation(Validation.VALIDATION_ID_REQUIRE);
        String message = null;
        Boolean value = true;
        if (lngBox == null || lngBox.getValue() == null) {
            value = false;
        }

        if (value) {
            lngBox.setStyle(setStyleEdit(lngBox.getStyle(), Boolean.TRUE));
        } else {
            lngBox.setStyle(setStyleEdit(lngBox.getStyle(), Boolean.FALSE));
            boolean addEventListener = lngBox.addEventListener(Events.ON_CHANGE, new ValidTextLongDoubleBoxEventListener(lngBox));
            message = ResourceUtil.getLabel("validate.message.require");
            if (!str.isEmpty()) {
                message = str;
            }
        }
        result.setValue(value);
        result.setMessage(message);
        return result;
    }

    public static Validation requireField(Intbox field1) {
        Validation result = new Validation(Validation.VALIDATION_ID_REQUIRE);
        String message = null;
        Boolean value = true;
        Integer data = field1.getValue();
        if (data == null) {
            value = false;
        }
        if (value) {
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.TRUE));
        } else {
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.FALSE));
            boolean addEventListener = field1.addEventListener(Events.ON_CHANGE, new ValidTextLongDoubleBoxEventListener(field1));
            message = ResourceUtil.getLabel("validate.message.require");
        }
        result.setValue(value);
        result.setMessage(message);
        return result;
    }

    public static Validation requireField(Intbox intbox, String massage) {
        Validation result = new Validation(Validation.VALIDATION_ID_REQUIRE);
        String message = null;
        Boolean value = true;
        Integer data = intbox.getValue();
        if (data == null) {
            value = false;
        }
        if (value) {
            intbox.setStyle(setStyleEdit(intbox.getStyle(), Boolean.TRUE));
        } else {
            intbox.setStyle(setStyleEdit(intbox.getStyle(), Boolean.FALSE));
            boolean addEventListener = intbox.addEventListener(Events.ON_CHANGE, new ValidTextLongDoubleBoxEventListener(intbox));
        }
        result.setValue(value);
        result.setMessage(message);
        return result;
    }

    public static Validation requireField(Doublebox doublebox, String massage) {
        Validation result = new Validation(Validation.VALIDATION_ID_REQUIRE);
        String message = null;
        Boolean value = true;
        Double data = doublebox.getValue();
        if (data == null) {
            value = false;
        }
        if (value) {
            doublebox.setStyle(setStyleEdit(doublebox.getStyle(), Boolean.TRUE));
        } else {
            doublebox.setStyle(setStyleEdit(doublebox.getStyle(), Boolean.FALSE));
            boolean addEventListener = doublebox.addEventListener(Events.ON_CHANGE, new ValidTextLongDoubleBoxEventListener(doublebox));
        }
        result.setValue(value);
        result.setMessage(message);
        return result;
    }

    public static Validation requireField(Datebox field1) {
        Validation result = new Validation(Validation.VALIDATION_ID_REQUIRE);
        String message = null;
        Boolean value = true;
        Date data = field1.getValue();
        if (data == null) {
            value = false;
        }
        if (value) {
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.TRUE));
        } else {
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.FALSE));
            message = ResourceUtil.getLabel("validate.message.require");
        }
        result.setValue(value);
        result.setMessage(message);
        return result;
    }

    public static Validation requireField(Datebox field1, String msg) {
        Validation result = new Validation(Validation.VALIDATION_ID_REQUIRE);
        String message = null;
        Boolean value = true;
        Date data = field1.getValue();
        if (data == null) {
            value = false;
        }
        if (value) {
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.TRUE));
        } else {
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.FALSE));
            field1.addEventListener(Events.ON_CHANGE, new ValidDateboxEventListener(field1));
            message = msg;
        }
        result.setValue(value);
        result.setMessage(message);
        return result;
    }

    public static Validation requireField(Combobox field1, String setWidth) {
        Validation result = new Validation(Validation.VALIDATION_ID_REQUIRE);
        String message = null;
        Boolean value = true;
        int data = field1.getSelectedIndex();
        if (data == 0 || data == -1) {
            if (data == -1) {
                value = false;
            } else {
                if (field1.getSelectedItem() != null) {
                    if (field1.getSelectedItem().getValue() == null
                            || "".equals(field1.getSelectedItem().getValue())) {
                        value = false;
                    }
                }
            }
        }
        if (value) {
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.TRUE));
        } else {
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.FALSE));
            field1.setWidth(setWidth);
            boolean addEventListener = field1.addEventListener(Events.ON_CHANGE, new ValidComboboxEventListener(field1));
            message = ResourceUtil.getLabel("validate.message.require");
        }
        result.setValue(value);
        result.setMessage(message);
        return result;
    }

    public static Validation requireField(Combobox field1) {
        Validation result = new Validation(Validation.VALIDATION_ID_REQUIRE);
        String message = null;
        Boolean value = true;
        int data = field1.getSelectedIndex();
        if (data == 0 || data == -1) {
            if (data == -1) {
                value = false;
            } else {
                if (field1.getSelectedItem() != null) {
                    if (field1.getSelectedItem().getValue() == null
                            || "".equals(field1.getSelectedItem().getValue())) {
                        value = false;
                    }
                }
            }
        }
        if (value) {
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.TRUE));
        } else {
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.FALSE));
            boolean addEventListener = field1.addEventListener(Events.ON_CHANGE, new ValidComboboxEventListener(field1));
            message = ResourceUtil.getLabel("validate.message.require");
        }
        result.setValue(value);
        result.setMessage(message);
        return result;
    }

    public static Validation requireField(Combobox field1, String msg, String setWidth) {
        Validation result = new Validation(Validation.VALIDATION_ID_REQUIRE);
        String message = null;
        Boolean value = true;
        int data = field1.getSelectedIndex();
        if (data == 0 || data == -1) {
            if (data == -1) {
                value = false;
            } else {
                if (field1.getSelectedItem() != null) {
                    if (field1.getSelectedItem().getValue() == null
                            || "".equals(field1.getSelectedItem().getValue())) {
                        value = false;
                    }
                } else {
                    value = false;
                }
            }
        }
        if (value) {
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.TRUE));
        } else {
            if (setWidth != null && !setWidth.equals("")) {
                field1.setWidth(setWidth);
            }
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.FALSE));
            message = msg;
            boolean addEventListener = field1.addEventListener(Events.ON_CHANGE, new ValidComboboxEventListener(field1));
            //LOG.info(addEventListener);

        }
        result.setValue(value);
        result.setMessage(message);
        return result;
    }

    public static Validation regularField(Textbox field1, String reg, String msg) {
        Validation result = new Validation(Validation.VALIDATION_ID_REGULAREXPRESSIONS);
        String message = null;
        Boolean value = true;
        String data = field1.getValue();
        if (data == null) {
            value = false;
        } else {
            if (StringUtils.isEmpty(data.trim())) {
                value = false;
            }
            if (!data.matches(reg)) {
                value = false;
            }
        }
        if (value) {
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.TRUE));
        } else {
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.FALSE));
            boolean addEventListener = field1.addEventListener(Events.ON_CHANGE, new ValidTextLongDoubleBoxEventListener(field1));
            //LOG.info(addEventListener);
            message = ResourceUtil.getLabel(msg);
        }
        result.setValue(value);
        result.setMessage(message);
        return result;
    }

    public static Validation regularField(Textbox field1, String reg) {
        Validation result = new Validation(Validation.VALIDATION_ID_REGULAREXPRESSIONS);
        String message = null;
        Boolean value = true;
        String data = field1.getValue();
        if (data == null) {
            value = false;
        } else {
            if (StringUtils.isEmpty(data.trim())) {
                value = false;
            }
            if (!data.matches(reg)) {
                value = false;
            }
        }
        if (value) {
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.TRUE));
        } else {
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.FALSE));
            boolean addEventListener = field1.addEventListener(Events.ON_CHANGE, new ValidTextLongDoubleBoxEventListener(field1));
            //LOG.info(addEventListener);
            message = ResourceUtil.getLabel("validate.message.regularexpression");
        }
        result.setValue(value);
        result.setMessage(message);
        return result;
    }

    public static Validation requireField(Radiogroup field1) {
        Validation result = new Validation(Validation.VALIDATION_ID_REQUIRE);
        String message = null;
        Boolean value = true;
        int index = field1.getSelectedIndex();
        if (index == -1) {
            value = false;
        }
        if (value) {
            for (Radio radio : field1.getItems()) {
                radio.setStyle(setStyleEdit(radio.getStyle(), Boolean.TRUE));
            }
        } else {
            for (Radio radio : field1.getItems()) {
                radio.setStyle(setStyleEdit(radio.getStyle(), Boolean.FALSE));
            }
            message = ResourceUtil.getLabel("validate.message.require");
        }
        result.setValue(value);
        result.setMessage(message);
        return result;
    }

    public static Validation requireField(Label field1, String msg) {
        Validation result = new Validation(Validation.VALIDATION_ID_REQUIRE);
        String message = null;
        Boolean value = true;
        if (field1.getValue() != null && !field1.getValue().equals("")) {
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.TRUE));
        } else {
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.FALSE));
        }
        message = msg;
        result.setValue(value);
        result.setMessage(message);
        return result;
    }

    public static Validation requireField(Label field1) {
        Validation result = new Validation(Validation.VALIDATION_ID_REQUIRE);
        String message = null;
        Boolean value = true;
        if (field1.getValue() != null && !field1.getValue().equals("")) {
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.TRUE));
        } else {
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.FALSE));
        }

        result.setValue(value);
        return result;
    }

    public static Validation requireField(Checkbox field1, String msg) {
        Validation result = new Validation(Validation.VALIDATION_ID_REQUIRE);
        Boolean value = true;
        if (field1 == null || !field1.isChecked()) {
            value = false;
        }
        if (value) {
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.TRUE));
        } else {
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.FALSE));
            boolean addEventListener = field1.addEventListener(Events.ON_CHECK, new ValidTextLongDoubleBoxEventListener(field1));
        }

        result.setValue(value);
        result.setMessage(msg);
        return result;
    }

    public static Validation requireField(Checkbox field1) {
        Validation result = new Validation(Validation.VALIDATION_ID_REQUIRE);
        Boolean value = true;
        if (field1 == null || !field1.isChecked()) {
            value = false;
        }
        if (value) {
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.TRUE));
        } else {
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.FALSE));
            boolean addEventListener = field1.addEventListener(Events.ON_CHECK, new ValidTextLongDoubleBoxEventListener(field1));
        }

        result.setValue(value);
        return result;
    }

    public static Validation requireField(Radiogroup field1, String msg) {
        Validation result = new Validation(Validation.VALIDATION_ID_REQUIRE);
        String message = null;
        Boolean value = true;
        int index = field1.getSelectedIndex();
        if (index == -1) {
            value = false;
        }
        if (value) {
            for (Radio radio : field1.getItems()) {
                radio.setStyle(setStyleEdit(radio.getStyle(), Boolean.TRUE));
            }
        } else {
            for (Radio radio : field1.getItems()) {
                radio.setStyle(setStyleEdit(radio.getStyle(), Boolean.FALSE));
            }
            message = msg;
        }
        result.setValue(value);
        result.setMessage(message);
        return result;
    }

    public static Validation requireFieldAndMoreThanOne(Intbox field1) {
        Validation result = new Validation(Validation.VALIDATION_ID_REQUIRE);
        String message = null;
        Boolean value = true;
        Integer data = field1.getValue();
        if (data == null || (data != null && data < 1)) {
            value = false;
        }
        if (value) {
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.TRUE));
        } else {
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.FALSE));
            message = ResourceUtil.getLabel("validate.message.require");
        }
        result.setValue(value);
        result.setMessage(message);
        return result;
    }

    public static Validation fieldMoreThanOne(Intbox field1) {
        Validation result = new Validation(Validation.VALIDATION_ID_REQUIRE);
        String message = null;
        Boolean value = true;
        Integer data = field1.getValue();
        if (data != null && data < 1) {
            value = false;
        }
        if (value) {
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.TRUE));
        } else {
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.FALSE));
            message = ResourceUtil.getLabel("validate.message.require");
        }
        result.setValue(value);
        result.setMessage(message);
        return result;
    }

    public static Validation compareStartDate(Datebox start, Datebox end) {
        return compareStartDate(start, end, Boolean.FALSE);
    }

    public static Validation compareStartDate(Datebox start, Datebox end, Boolean equalAllowed) {
        Validation result = new Validation(Validation.VALIDATION_ID_COMPARESTARTDATE);
        String message = null;
        Boolean value = true;

        Date startDate = start.getValue();
        Date endDate = end.getValue();
        if (!(startDate == null && endDate == null)) {
            if (startDate == null || endDate == null) {
                value = false;
                if (startDate == null) {
                    start.setStyle(setStyleEdit(start.getStyle(), Boolean.FALSE));
                    message = ResourceUtil.getLabel("validate.message.date.require.start");
                } else {
                    start.setStyle(setStyleEdit(start.getStyle(), Boolean.TRUE));
                }
                if (endDate == null) {
                    end.setStyle(setStyleEdit(end.getStyle(), Boolean.FALSE));
                    message = ResourceUtil.getLabel("validate.message.date.require.end");
                } else {
                    end.setStyle(setStyleEdit(end.getStyle(), Boolean.TRUE));
                }
            } else {
                if (startDate.compareTo(endDate) > 0 - (equalAllowed ? 0 : 1)) {
                    value = false;
                    end.setStyle(setStyleEdit(end.getStyle(), Boolean.FALSE));
                    message = ResourceUtil.getLabel("validate.message.date.compare.startend");
                } else {
                    end.setStyle(setStyleEdit(end.getStyle(), Boolean.TRUE));
                }
            }
        }

        result.setValue(value);
        result.setMessage(message);
        return result;
    }

    public static Validation compareDateLinear(Datebox start, Datebox end) {
        Validation result = new Validation(Validation.VALIDATION_ID_COMPARESTARTDATE);
        String message = null;
        Boolean value = true;
        Date stDate = start.getValue();
        Date fnDate = end.getValue();
        if (stDate != null && fnDate != null) {
            if (((fnDate.getTime() - stDate.getTime()) / 86400000L) != 1) {
                value = false;
                start.setStyle(setStyleEdit(start.getStyle(), Boolean.FALSE));
                end.setStyle(setStyleEdit(end.getStyle(), Boolean.FALSE));
                message = ResourceUtil.getLabel("validate.message.date.compare.startend");
            } else {
                start.setStyle(setStyleEdit(start.getStyle(), Boolean.TRUE));
                end.setStyle(setStyleEdit(end.getStyle(), Boolean.TRUE));
            }
        } else {
            start.setStyle(setStyleEdit(start.getStyle(), Boolean.FALSE));
            end.setStyle(setStyleEdit(end.getStyle(), Boolean.FALSE));
            value = false;
            if (stDate == null) {
                message = ResourceUtil.getLabel("validate.message.date.require.start");
            }
            if (fnDate == null) {
                message = ResourceUtil.getLabel("validate.message.date.require.end");
            }
        }

        result.setValue(value);
        result.setMessage(message);
        return result;
    }

    public static Validation compareLongValue(Textbox start, Textbox end) {
        Validation result = new Validation(Validation.VALIDATION_ID_COMPAREVALUE);
        String message = null;
        Boolean value = true;
        if (start.getValue() != null && end.getValue() != null) {
            try {
                Long startVal = Long.parseLong(start.getValue());
                Long endVal = Long.parseLong(end.getValue());
                if (startVal.compareTo(endVal) == 1) {
                    start.setStyle(setStyleEdit(start.getStyle(), Boolean.FALSE));
                    end.setStyle(setStyleEdit(end.getStyle(), Boolean.FALSE));
                    value = false;
                    message = ResourceUtil.getLabel("validate.message.long.compare.startend");
                } else {
                    start.setStyle(setStyleEdit(start.getStyle(), Boolean.TRUE));
                    end.setStyle(setStyleEdit(end.getStyle(), Boolean.TRUE));
                }
            } catch (Exception e) {
                start.setStyle(setStyleEdit(start.getStyle(), Boolean.FALSE));
                end.setStyle(setStyleEdit(end.getStyle(), Boolean.FALSE));
                value = false;
                message = ResourceUtil.getLabel("validate.message.long.compare.startend");
            }
        } else if (start.getValue() != null) {
            try {
                Long startVal = Long.parseLong(start.getValue());
            } catch (Exception e) {
                start.setStyle(setStyleEdit(start.getStyle(), Boolean.FALSE));
                value = false;
                message = ResourceUtil.getLabel("validate.message.long.compare.startend");
            }
        } else if (end.getValue() != null) {
            try {
                Long endVal = Long.parseLong(start.getValue());
            } catch (Exception e) {
                end.setStyle(setStyleEdit(end.getStyle(), Boolean.FALSE));
                value = false;
                message = ResourceUtil.getLabel("validate.message.long.compare.startend");
            }
        } else {
            start.setStyle(setStyleEdit(start.getStyle(), Boolean.FALSE));
            end.setStyle(setStyleEdit(end.getStyle(), Boolean.FALSE));
            value = false;
            message = ResourceUtil.getLabel("validate.message.long.require.start");
        }

        result.setValue(value);
        result.setMessage(message);
        return result;
    }

    public static Validation compareIntValue(Intbox start, Intbox end) {
        Validation result = new Validation(Validation.VALIDATION_ID_COMPAREVALUE);
        String message = null;
        Boolean value = true;
        if (start.getValue() != null && end.getValue() != null) {
            try {
                Integer startVal = start.getValue();
                Integer endVal = end.getValue();
                if (startVal.compareTo(endVal) == 1) {
                    start.setStyle(setStyleEdit(start.getStyle(), Boolean.FALSE));
                    end.setStyle(setStyleEdit(end.getStyle(), Boolean.FALSE));
                    value = false;
                    message = ResourceUtil.getLabel("validate.message.int.compare.startend");
                } else {
                    start.setStyle(setStyleEdit(start.getStyle(), Boolean.TRUE));
                    end.setStyle(setStyleEdit(end.getStyle(), Boolean.TRUE));
                }
            } catch (Exception e) {
                start.setStyle(setStyleEdit(start.getStyle(), Boolean.FALSE));
                end.setStyle(setStyleEdit(end.getStyle(), Boolean.FALSE));
                value = false;
                message = ResourceUtil.getLabel("validate.message.int.compare.startend");
            }
        } else if (start.getValue() != null) {
            try {
                Integer startVal = start.getValue();
            } catch (Exception e) {
                start.setStyle(setStyleEdit(start.getStyle(), Boolean.FALSE));
                value = false;
                message = ResourceUtil.getLabel("validate.message.int.compare.startend");
            }
        } else if (end.getValue() != null) {
            try {
                Integer endVal = end.getValue();
            } catch (Exception e) {
                end.setStyle(setStyleEdit(end.getStyle(), Boolean.FALSE));
                value = false;
                message = ResourceUtil.getLabel("validate.message.int.compare.startend");
            }
        } else {
            start.setStyle(setStyleEdit(start.getStyle(), Boolean.FALSE));
            end.setStyle(setStyleEdit(end.getStyle(), Boolean.FALSE));
            value = false;
            message = ResourceUtil.getLabel("validate.message.int.require.start");
        }

        result.setValue(value);
        result.setMessage(message);
        return result;
    }

    public static Validation compareBigDecimalValue(Decimalbox start, Decimalbox end) {
        Validation result = new Validation(Validation.VALIDATION_ID_COMPAREVALUE);
        String message = null;
        Boolean value = true;
        if (start.getValue() != null && end.getValue() != null) {
            try {
                BigDecimal startVal = start.getValue();
                BigDecimal endVal = end.getValue();
                if (startVal.compareTo(endVal) == 1) {
                    start.setStyle(setStyleEdit(start.getStyle(), Boolean.FALSE));
                    end.setStyle(setStyleEdit(end.getStyle(), Boolean.FALSE));
                    value = false;
                    message = ResourceUtil.getLabel("validate.message.decimal.compare.startend");
                } else {
                    start.setStyle(setStyleEdit(start.getStyle(), Boolean.TRUE));
                    end.setStyle(setStyleEdit(end.getStyle(), Boolean.TRUE));
                }
            } catch (Exception e) {
                start.setStyle(setStyleEdit(start.getStyle(), Boolean.FALSE));
                end.setStyle(setStyleEdit(end.getStyle(), Boolean.FALSE));
                value = false;
                message = ResourceUtil.getLabel("validate.message.decimal.compare.startend");
            }
        } else {
            start.setStyle(setStyleEdit(start.getStyle(), Boolean.FALSE));
            end.setStyle(setStyleEdit(end.getStyle(), Boolean.FALSE));
            value = false;
            message = ResourceUtil.getLabel("validate.message.decimal.require.start");
        }

        result.setValue(value);
        result.setMessage(message);
        return result;
    }

    public static Validation requireCompareValue(Textbox field1, Textbox field2) {
        Validation result = new Validation(Validation.VALIDATION_ID_COMPAREVALUE);
        String message = null;
        Boolean value = true;

        String data1 = field1.getValue();
        String data2 = field2.getValue();

        if (data1 == null || data2 == null) {
            value = false;
            message = ResourceUtil.getLabel("validate.message.require");
            if (data1 == null) {
                field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.FALSE));
            } else {
                field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.TRUE));
            }
            if (data2 == null) {
                field2.setStyle(setStyleEdit(field2.getStyle(), Boolean.FALSE));
            } else {
                field2.setStyle(setStyleEdit(field2.getStyle(), Boolean.TRUE));
            }
        } else {
            if (!data1.equals(data2)) {
                value = false;
                field2.setStyle(setStyleEdit(field2.getStyle(), Boolean.FALSE));
                message = ResourceUtil.getLabel("validate.message.equal");
            } else {
                field2.setStyle(setStyleEdit(field2.getStyle(), Boolean.TRUE));
            }
        }

        result.setValue(value);
        result.setMessage(message);
        return result;
    }

    public static Validation requireCompareLength(Intbox field1, Intbox field2) {
        Validation result = new Validation(Validation.VALIDATION_ID_COMPARELENGHT);
        String message = null;
        Boolean value = true;
        Integer lenght1 = field1.getText().length();
        Integer lenght2 = field2.getText().length();
        if (lenght1 != lenght2 || lenght1 == 0 || lenght2 == 0) {
            value = false;
            if (lenght1 == 0 || lenght2 == 0) {
                message = ResourceUtil.getLabel("validate.message.equal");
                if (lenght1 == 0) {
                    field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.FALSE));
                }
                if (lenght2 == 0) {
                    field2.setStyle(setStyleEdit(field2.getStyle(), Boolean.FALSE));
                }
            } else {
                message = ResourceUtil.getLabel("validate.message.unequal.size");
                field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.FALSE));
                field2.setStyle(setStyleEdit(field2.getStyle(), Boolean.FALSE));
            }
        } else {
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.TRUE));
            field2.setStyle(setStyleEdit(field2.getStyle(), Boolean.TRUE));
        }

        result.setValue(value);
        result.setMessage(message);
        return result;
    }

    public static Validation requireCompareLength(Textbox field1, Textbox field2) {
        Validation result = new Validation(Validation.VALIDATION_ID_COMPARELENGHT);
        String message = null;
        Boolean value = true;
        Integer lenght1 = field1.getText().length();
        Integer lenght2 = field2.getText().length();
        if (lenght1 != lenght2 || lenght1 == 0 || lenght2 == 0) {
            value = false;
            if (lenght1 == 0 || lenght2 == 0) {
                message = ResourceUtil.getLabel("validate.message.equal");
                if (lenght1 == 0) {
                    field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.FALSE));
                }
                if (lenght2 == 0) {
                    field2.setStyle(setStyleEdit(field2.getStyle(), Boolean.FALSE));
                }
            } else {
                message = ResourceUtil.getLabel("validate.message.unequal.size");
                field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.FALSE));
                field2.setStyle(setStyleEdit(field2.getStyle(), Boolean.FALSE));
            }
        } else {
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.TRUE));
            field2.setStyle(setStyleEdit(field2.getStyle(), Boolean.TRUE));
        }

        result.setValue(value);
        result.setMessage(message);
        return result;
    }

    public static Validation requireMinChar(Textbox field1, Integer minVal, String msg) {
        Validation result = new Validation(Validation.VALIDATION_ID_REQUIRE);
        String message = null;
        Boolean value = true;
        String data1 = field1.getValue();
        if (data1 == null) {
            value = false;
            message = ResourceUtil.getLabel("validate.message.require");
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.FALSE));

        }

        if (data1.length() < minVal) {
            value = false;
            message = ResourceUtil.getLabel(msg);
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.FALSE));
        }

        result.setValue(value);
        result.setMessage(message);
        return result;
    }

    public static Validation requireMinChar(Longbox field1, Integer minVal, String msg) {
        Validation result = new Validation(Validation.VALIDATION_ID_REQUIRE);
        String message = null;
        Boolean value = true;
        String data1 = String.valueOf(field1.getValue());
        if (data1 == null) {
            value = false;
            message = ResourceUtil.getLabel("validate.message.require");
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.FALSE));
        }

        if (data1.length() < minVal) {
            value = false;
            message = ResourceUtil.getLabel(msg);
            field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.FALSE));
            Boolean addEventListener = field1.addEventListener(Events.ON_CHANGE, new ValidMinCharEventListener(field1, minVal));
        }

        result.setValue(value);
        result.setMessage(message);
        return result;
    }

//    public static Validation checkPassword(Long userId,Textbox field1){
//        Validation result = new Validation(Validation.VALIDATION_ID_PASSWORD);
//        String message = null;
//        Boolean value = true;
//        try{
//            String password = field1.getText();
//            DAOFactory factory = new DAOFactory<UserDAO>();
//            UserDAO dao = (UserDAO) factory.createDAO("user");
//            User user = (User)dao.select(userId);
//            Boolean data = user.getPassword().equals(KeyConverter.encode(password, KeyConverter.ALGORITHM_AES));
//            if(data){
//                value = true;
//                field1.setStyle(setStyleEdit(.getStyle(), Boolean.TRUE));
//            }else{
//                value = false;
//                field1.setStyle(setStyleEdit(.getStyle(), Boolean.FALSE));
//                message = ResourceUtil.getLabel("validate.message.password");
//            }
//        }catch(Exception e){
//            LOG.error(e.toString());
//        }
//        result.setValue(value);
//        result.setMessage(message);
//        return result;
//    }
//
//
//    public static Validation verifyCaptcha(Captcha captcha,Textbox field){
//        Validation result = new Validation(Validation.VALIDATION_ID_CAPTCHA);
//        String message = null;
//        Boolean value = true;
//        if (StringUtils.equals(field.getValue(), captcha.getAnswer())) {
//            value = true;
//            field.setStyle(setStyleEdit(.getStyle(), Boolean.TRUE));
//	}else{
//            value = false;
//            field.setStyle(setStyleEdit(.getStyle(), Boolean.FALSE));
//            message = ResourceUtil.getLabel("validate.message.captcha");
//        }
//        result.setValue(value);
//        result.setMessage(message);
//        return result;
//    }
    // Adhoc error without specific field
    public static Validation addError(String msg) {
        Validation result = new Validation(0); // Adhoc error
        String message = msg;
        Boolean value = false;
        result.setValue(value);
        result.setMessage(message);

        return result;
    }

    public static Validation addError(Textbox field1, String msg) {
        Validation result = new Validation(Validation.VALIDATION_ID_REQUIRE);
        String message = msg;
        Boolean value = false;
        Boolean addEventListener = field1.addEventListener(Events.ON_CHANGE, new ValidTextLongDoubleBoxEventListener(field1));
        field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.FALSE));
        result.setValue(value);
        result.setMessage(message);

        return result;
    }

    public static Validation addError(Intbox field1, String msg) {
        Validation result = new Validation(Validation.VALIDATION_ID_REQUIRE);
        String message = msg;
        Boolean value = false;
        Boolean addEventListener = field1.addEventListener(Events.ON_CHANGE, new ValidTextLongDoubleBoxEventListener(field1));
        field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.FALSE));
        result.setValue(value);
        result.setMessage(message);

        return result;
    }

    public static Validation addError(Longbox field1, String msg) {
        Validation result = new Validation(Validation.VALIDATION_ID_REQUIRE);
        String message = msg;
        Boolean value = false;
        Boolean addEventListener = field1.addEventListener(Events.ON_CHANGE, new ValidTextLongDoubleBoxEventListener(field1));
        field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.FALSE));
        result.setValue(value);
        result.setMessage(message);

        return result;
    }

    public static Validation addError(Doublebox field1, String msg) {
        Validation result = new Validation(Validation.VALIDATION_ID_REQUIRE);
        String message = msg;
        Boolean value = false;
        Boolean addEventListener = field1.addEventListener(Events.ON_CHANGE, new ValidTextLongDoubleBoxEventListener(field1));
        field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.FALSE));
        result.setValue(value);
        result.setMessage(message);

        return result;
    }

    public static Validation addError(Decimalbox field1, String msg) {
        Validation result = new Validation(Validation.VALIDATION_ID_REQUIRE);
        String message = msg;
        Boolean value = false;
        Boolean addEventListener = field1.addEventListener(Events.ON_CHANGE, new ValidTextLongDoubleBoxEventListener(field1));
        field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.FALSE));
        result.setValue(value);
        result.setMessage(message);

        return result;
    }

    public static Validation addError(Datebox field1, String msg) {
        Validation result = new Validation(Validation.VALIDATION_ID_REQUIRE);
        String message = msg;
        Boolean value = false;
        Boolean addEventListener = field1.addEventListener(Events.ON_CHANGE, new ValidTextLongDoubleBoxEventListener(field1));
        field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.FALSE));
        result.setValue(value);
        result.setMessage(message);

        return result;
    }

    public static Validation addError(Timebox field1, String msg) {
        Validation result = new Validation(Validation.VALIDATION_ID_REQUIRE);
        String message = msg;
        Boolean value = false;
        Boolean addEventListener = field1.addEventListener(Events.ON_CHANGE, new ValidTextLongDoubleBoxEventListener(field1));
        field1.setStyle(setStyleEdit(field1.getStyle(), Boolean.FALSE));
        result.setValue(value);
        result.setMessage(message);

        return result;
    }

    private static class ValidTextLongDoubleBoxEventListener implements EventListener {

        private Textbox textbox;
        private Intbox intbox;
        private Longbox longbox;
        private Doublebox doublebox;
        private Decimalbox decimalbox;
        private Datebox datebox;
        private Timebox timebox;
        private Checkbox checkbox;

        ValidTextLongDoubleBoxEventListener(Textbox textbox) {
            this.textbox = textbox;
        }

        ValidTextLongDoubleBoxEventListener(Intbox intbox) {
            this.intbox = intbox;
        }

        ValidTextLongDoubleBoxEventListener(Longbox longbox) {
            this.longbox = longbox;
        }

        ValidTextLongDoubleBoxEventListener(Doublebox doublebox) {
            this.doublebox = doublebox;
        }

        ValidTextLongDoubleBoxEventListener(Decimalbox decimalbox) {
            this.decimalbox = decimalbox;
        }

        ValidTextLongDoubleBoxEventListener(Datebox datebox) {
            this.datebox = datebox;
        }

        ValidTextLongDoubleBoxEventListener(Timebox timebox) {
            this.timebox = timebox;
        }

        ValidTextLongDoubleBoxEventListener(Checkbox checkbox) {
            this.checkbox = checkbox;
        }

        @Override
        public void onEvent(Event event) throws Exception {
            if (textbox != null) {
//                textbox.setStyle(setStyleEdit(.getStyle(), Boolean.TRUE));
                textbox.setStyle(setStyleEdit(textbox.getStyle(), Boolean.TRUE));
            }
            if (intbox != null) {
//                intbox.setStyle(setStyleEdit(.getStyle(), Boolean.TRUE));
                intbox.setStyle(setStyleEdit(intbox.getStyle(), Boolean.TRUE));
            }
            if (longbox != null) {
//                longbox.setStyle(setStyleEdit(.getStyle(), Boolean.TRUE));
                longbox.setStyle(setStyleEdit(longbox.getStyle(), Boolean.TRUE));
            }
            if (doublebox != null) {
//                doublebox.setStyle(setStyleEdit(.getStyle(), Boolean.TRUE));
                doublebox.setStyle(setStyleEdit(doublebox.getStyle(), Boolean.TRUE));
            }
            if (decimalbox != null) {
//                decimalbox.setStyle(setStyleEdit(.getStyle(), Boolean.TRUE));
                decimalbox.setStyle(setStyleEdit(decimalbox.getStyle(), Boolean.TRUE));
            }
            if (datebox != null) {
//                datebox.setStyle(setStyleEdit(.getStyle(), Boolean.TRUE));
                datebox.setStyle(setStyleEdit(datebox.getStyle(), Boolean.TRUE));
            }
            if (timebox != null) {
//                timebox.setStyle(setStyleEdit(.getStyle(), Boolean.TRUE));
                timebox.setStyle(setStyleEdit(timebox.getStyle(), Boolean.TRUE));
            }
            if (checkbox != null) {
                checkbox.setStyle(setStyleEdit(checkbox.getStyle(), Boolean.TRUE));
            }
        }
    }

    private static class ValidMinCharEventListener implements EventListener {

        private Textbox textbox;
        private Longbox longbox;
        private Integer minVal;

        ValidMinCharEventListener(Textbox textbox, Integer minVal) {
            this.textbox = textbox;
            this.minVal = minVal;
        }

        ValidMinCharEventListener(Longbox textbox, Integer minVal) {
            this.longbox = textbox;
            this.minVal = minVal;
        }

        @Override
        public void onEvent(Event event) throws Exception {

            if (textbox != null) {
                if (textbox.getValue().length() == minVal) {
                    textbox.setStyle(setStyleEdit(textbox.getStyle(), Boolean.TRUE));
                }
            }
            if (longbox != null) {
                if (String.valueOf(longbox.getValue()).length() == minVal) {
                    longbox.setStyle(setStyleEdit(longbox.getStyle(), Boolean.TRUE));
                }
            }
        }
    }

    private static class ValidComboboxEventListener implements EventListener {

        private Combobox combobox;

        ValidComboboxEventListener(Combobox cmb) {
            this.combobox = cmb;
        }

        @Override
        public void onEvent(Event event) throws Exception {

            combobox.setStyle(setStyleEdit(combobox.getStyle(), Boolean.TRUE));

        }
    }

    private static class ValidDecimalboxEventListener implements EventListener {

        private Decimalbox decimalbox;

        ValidDecimalboxEventListener(Decimalbox decimalbox) {
            this.decimalbox = decimalbox;
        }

        @Override
        public void onEvent(Event event) throws Exception {

//            decimalbox.setStyle(setStyleEdit(.getStyle(), Boolean.TRUE));
            decimalbox.setStyle(setStyleEdit(decimalbox.getStyle(), Boolean.TRUE));
        }
    }

    private static class ValidDateboxEventListener implements EventListener {

        private Datebox datebox;

        ValidDateboxEventListener(Datebox datebox) {
            this.datebox = datebox;
        }

        @Override
        public void onEvent(Event event) throws Exception {

            datebox.setStyle(setStyleEdit(datebox.getStyle(), Boolean.TRUE));



        }
    }

    public static String setStyleEdit(String style, Boolean isValid) {

        if (null == style) {
            style = "";
        }

        style = style.replace(INVALID, "");

        if (!isValid) {
            style = (style + INVALID);
        }

        return style;

    }
}
